package xcmg.device.service.warehouse;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.warehouse.DefaultWarehouseDO;
import xcmg.device.dao.entity.warehouse.WarehouseAreaDO;
import xcmg.device.dao.entity.warehouse.WarehouseAreaLocationDO;
import xcmg.device.dao.entity.warehouse.WarehouseDO;
import xcmg.device.dao.mapper.warehouse.DefaultWarehouseDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseAreaDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseAreaLocationDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseDOMapper;
import xcmg.device.infra.BeanUtil;
import xcmg.device.infra.exception.CommonBusiExceptions;
import xcmg.device.service.basic.CommonGenService;
import xcmg.device.service.vo.warehouse.DefaultWarehouseVO;
import xcmg.device.util.CommonUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@RefreshScope
public class WarehouseCompose {

    @Value("${company.setting.defaultWarehouse:hj001-1-1-1-1-kq01-DEFAULT}")
    private String defaultWarehouseLocation;

    @Value("${company.setting.defaultZbAreaCode:ZBKQ}")
    private String defaultZbAreaCode;

    @Autowired
    private WarehouseDOMapper warehouseDOMapper;

    @Autowired
    private WarehouseAreaDOMapper warehouseAreaDOMapper;

    @Autowired
    private WarehouseAreaLocationDOMapper warehouseAreaLocationDOMapper;

    @Autowired
    private DefaultWarehouseDOMapper defaultWarehouseDOMapper;

    @Autowired
    private CommonGenService commonGenService;

    public DefaultWarehouseVO queryDefaultWarehouseVO(String orgId) {
        //查询有配置默认的仓库库区储位，使用默认的仓库库区储位
        DefaultWarehouseVO dvo = queryDefaultWarehouseVOByOrgId(orgId);
        if (null == dvo) {
            String warehouse = CommonUtils.getWarehouseCode(defaultWarehouseLocation);
            if (StringUtils.isBlank(warehouse)) {
                throw new CommonBusiExceptions(orgId + "默认仓库格式未配置,建默认仓库库区储位失败");
            }
            WarehouseDO warehouseDO = warehouseDOMapper.itemByWarehouseCode(orgId, warehouse);
            if (null == warehouseDO) {
                log.info("{}默认仓库未创建", orgId);
                return dvo;
            }
            dvo = new DefaultWarehouseVO();
            dvo.setWarehouseId(warehouseDO.getId());
            dvo.setWarehouseCode(warehouseDO.getWarehouseCode());

            String areaCode = CommonUtils.getWarehouseAreaCode(defaultWarehouseLocation);
            WarehouseAreaDO area = warehouseAreaDOMapper.itemByCode(orgId, areaCode, warehouseDO.getId());
            if (null == area) {
                log.info("{}默认库区", orgId);
                return dvo;
            }
            dvo.setAreaCode(area.getAreaCode());
            dvo.setAreaId(area.getId());

            WarehouseAreaLocationDO location = warehouseAreaLocationDOMapper.item(defaultWarehouseLocation, orgId);
            if (null == location) {
                log.info("{}默认储位", orgId);
                return dvo;
            }
            dvo.setLocationCode(location.getLocationCode());
        }
        return dvo;
    }

    private DefaultWarehouseVO queryDefaultWarehouseVOByOrgId(String orgId) {
        DefaultWarehouseVO vo = new DefaultWarehouseVO();
        vo.setOrgId(orgId);
        List<DefaultWarehouseVO> vos = queryDefaultWarehouseVOList(vo);
        if (CollectionUtils.isNotEmpty(vos)) {
            return vos.get(0);
        }
        return null;
    }

    private List<DefaultWarehouseVO> queryDefaultWarehouseVOList(DefaultWarehouseVO vo) {
        List<DefaultWarehouseDO> ll = defaultWarehouseDOMapper.queryList(vo);
        if (CollectionUtils.isNotEmpty(ll)) {
            return BeanUtil.do2bo4List(ll, DefaultWarehouseVO.class);
        }
        return null;
    }

    public String addDefaultWarehouseLocationWithoutE(String orgId) {
        try {
            return addDefaultWarehouseLocation(orgId);
        } catch (Exception e) {
            log.warn("", e);
            return e.getMessage();
        }
    }

    public String addDefaultWarehouseLocation(String orgId) {
        String warehouse = CommonUtils.getWarehouseCode(defaultWarehouseLocation);
        if (StringUtils.isBlank(warehouse)) {
            throw new CommonBusiExceptions(orgId + "默认仓库格式未配置,建默认仓库库区储位失败");
        }
        StringBuilder sb = new StringBuilder();
        WarehouseDO warehouseDO = warehouseDOMapper.itemByWarehouseCode(orgId, warehouse);
        if (null == warehouseDO) {
            warehouseDO = genWarehouseDO(warehouse, orgId);
            warehouseDOMapper.insert(warehouseDO);
            String areaCode = CommonUtils.getWarehouseAreaCode(defaultWarehouseLocation);
            WarehouseAreaDO area = genWarehouseAreaDO(orgId, areaCode, warehouseDO);
            warehouseAreaDOMapper.insert(area);
            WarehouseAreaLocationDO location = genWarehouseAreaLocationDO(defaultWarehouseLocation, orgId, area);
            warehouseAreaLocationDOMapper.insert(location);
        } else {
            log.warn("{} 企业已经存在默认仓库", orgId);
            sb.append(orgId + "企业已经存在默认仓库");
            String areaCode = CommonUtils.getWarehouseAreaCode(defaultWarehouseLocation);
            WarehouseAreaDO area = warehouseAreaDOMapper.itemByCode(orgId, areaCode, warehouseDO.getId());
            if (null == area) {
                area = genWarehouseAreaDO(orgId, areaCode, warehouseDO);
                warehouseAreaDOMapper.insert(area);
                WarehouseAreaLocationDO location = genWarehouseAreaLocationDO(defaultWarehouseLocation, orgId, area);
                warehouseAreaLocationDOMapper.insert(location);
            } else {
                log.warn("{} 企业已经存在默认仓库库区", orgId);
                sb.append(orgId + "企业已经存在默认仓库库区");
                WarehouseAreaLocationDO location = warehouseAreaLocationDOMapper.item(defaultWarehouseLocation, orgId);
                if (null == location) {
                    location = genWarehouseAreaLocationDO(defaultWarehouseLocation, orgId, area);
                    warehouseAreaLocationDOMapper.insert(location);
                } else {
                    log.warn("{} 企业已经存在默认仓库库区储位", orgId);
                    sb.append(orgId + "企业已经存在默认仓库库区储位");
                }
            }
        }
        return sb.toString();
    }

    private WarehouseAreaLocationDO genWarehouseAreaLocationDO(String locationCode, String orgId, WarehouseAreaDO area) {
        WarehouseAreaLocationDO locationDO = new WarehouseAreaLocationDO();
        locationDO.setLocationCode(locationCode);
        locationDO.setWarehouseAreaId(area.getId());
        String first = "1";
        locationDO.setRowNo(first);
        locationDO.setFloorNo(first);
        locationDO.setColumnNo(first);
        locationDO.setSeqNo(first);
        locationDO.setShelfType("hj");
        locationDO.setShelfNo("hj");
        locationDO.setOrgId(orgId);
        locationDO.setLocationDesc("1号货架1排1层1列1位");
        locationDO.setCreateTime(new Date());
        return locationDO;
    }

    private WarehouseAreaDO genWarehouseAreaDO(String orgId, String areaCode, WarehouseDO warehouseDO) {
        WarehouseAreaDO warehouseAreaDO = new WarehouseAreaDO();
        warehouseAreaDO.setId(commonGenService.textIdWithException());
        warehouseAreaDO.setAreaCode(areaCode);
        warehouseAreaDO.setWarehouseId(warehouseDO.getId());
        warehouseAreaDO.setAttrId(null);
        warehouseAreaDO.setAttrDesc(null);
        warehouseAreaDO.setDescription(null);
        warehouseAreaDO.setSapCode(null);
        warehouseAreaDO.setOrgId(orgId);
        return warehouseAreaDO;
    }

    private WarehouseDO genWarehouseDO(String warehouse, String orgId) {
        WarehouseDO warehouseDO = new WarehouseDO();
        warehouseDO.setId(commonGenService.textIdWithException());
        warehouseDO.setWarehouseCode(warehouse);
        warehouseDO.setOrgId(orgId);
        warehouseDO.setDescription("");
        warehouseDO.setKeeperId(null);
        warehouseDO.setIsPlan(0);
        warehouseDO.setIsCenter(0);
        warehouseDO.setFirstInOut(0);
        warehouseDO.setIsBarCode(1);
        warehouseDO.setWarehouseType(null);
        warehouseDO.setWarehouseTypeDesc(null);
        return warehouseDO;
    }

    public DefaultWarehouseVO queryDefaultWarehouseLocation(String orgId, String warehouse) {
        if (orgId == null || orgId.trim().isEmpty()) {
            throw new IllegalArgumentException("orgId不能为空");
        }
        if (warehouse == null || warehouse.trim().isEmpty()) {
            throw new IllegalArgumentException("warehouse不能为空");
        }

        WarehouseDO warehouseDO = warehouseDOMapper.itemByWarehouseCode(orgId, warehouse);
        if (null == warehouseDO) {
            throw new CommonBusiExceptions("组织"+orgId+"仓库未配置");
        }
        DefaultWarehouseVO dvo = new DefaultWarehouseVO();
        dvo.setWarehouseId(warehouseDO.getId());
        dvo.setWarehouseCode(warehouseDO.getWarehouseCode());
        WarehouseAreaDO area = warehouseAreaDOMapper.itemByCode(orgId, defaultZbAreaCode, warehouseDO.getId());
        if (null == area) {
            log.warn("{}默认库区未配置", orgId);
            return dvo;
        }
        dvo.setAreaCode(area.getAreaCode());
        dvo.setAreaId(area.getId());
        List<WarehouseAreaLocationDO> ll = warehouseAreaLocationDOMapper.list(area.getId());
        if (null == ll || ll.isEmpty()) {
            log.warn("{}默认库位未配置", orgId);
            return dvo;
        }
        dvo.setLocationCode(ll.get(0).getLocationCode());
        return dvo;
    }
}
